﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmModifyAllProductPrice : Form
    {
        public FrmModifyAllProductPrice(Shop_New_Info inShop)
        {
            InitializeComponent();
            ModelShop = inShop;
        }
        private Shop_New_Info ModelShop;
        private int ModelShopId = 0;
        ModifyProductPriceBll _bll = new ModifyProductPriceBll();
        List<Shop_New_Info> listShop = new List<Shop_New_Info>();
        BackgroundWorker backgroundWorkerModefy = new BackgroundWorker();
        int[] ModefyContent ={ 1,1,1};//调整内容
        int[] ModefyType = { 1, 1, 1 };//调整方式
        private void Init()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            textBoxShopName.Text = ModelShop.ShopName;
            listShop = _bll.GetAllShopList();
            listShop.ForEach(S => S.IsSelect = false);
            gridControlShop.DataSource = listShop;
            backgroundWorkerModefy.WorkerReportsProgress = true;
            backgroundWorkerModefy.WorkerSupportsCancellation = true;
            backgroundWorkerModefy.DoWork += new DoWorkEventHandler(backgroundWorkerModefy_DoWork);
            backgroundWorkerModefy.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorkerModefy_ProgressChanged);
            backgroundWorkerModefy.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorkerModefy_RunWorkerCompleted);
        }

        private void FrmModifyAllProductPrice_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void gridControlShop_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            listShop.ForEach(p=>p.IsSelect=true);
            gridControlShop.RefreshDataSource();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listShop.ForEach(p => p.IsSelect = !p.IsSelect);
            gridControlShop.RefreshDataSource();
        }
        List<int> shopIDList = new List<int>();
        private void buttonOk_Click(object sender, EventArgs e)
        {
            List<Shop_New_Info> surList = listShop.Where(f => f.IsSelect).ToList();
            ModefyContent[0] = Convert.ToInt32(checkBoxModefyProductPrice.Checked);
            ModefyContent[1] = (Convert.ToInt32(checkBoxModefyPaperprice.Checked));
            ModefyContent[2] = (Convert.ToInt32(checkBoxModefyCommonItemPrice.Checked));
            ModefyType[0] = Convert.ToInt32(checkBoxCostType.Checked);
            ModefyType[1] = Convert.ToInt32(checkBoxPrice.Checked);
            ModefyType[2] = Convert.ToInt32(checkBoxUpdateExist.Checked);
           
            ModelShopId = ModelShop.ShopID;
            surList.ForEach(p=>shopIDList.Add(p.ShopID));
            backgroundWorkerModefy.RunWorkerAsync(shopIDList);
            this.Cursor = Cursors.WaitCursor;
            
        }

        private void backgroundWorkerModefy_DoWork(object sender, DoWorkEventArgs e)
        {
           
            try
            {
                List<int> surList = shopIDList;



                backgroundWorkerModefy.ReportProgress(1, "0");
                int iCount = 0;//循环计数
                foreach (int shopidModefy in surList)
                {
                    int aaa = _bll.ModefyShopProduct(ModelShopId, shopidModefy, ModefyContent, ModefyType);

                    backgroundWorkerModefy.ReportProgress(1, ((iCount / surList.Count) * 100).ToString("0"));
                    iCount++;
                }
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
               
            }

        }

        private void backgroundWorkerModefy_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                int present = Convert.ToInt32(e.UserState);
                if (present > 100)
                {
                    present = 100;
                }
                progressBar1.Value = present;
            }


        }

        private void backgroundWorkerModefy_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        

       
    }
}
